<template>
  <div class="notification-container">
    <div class="notification-header">
      <div class="header-left">
        <button class="back-button" @click="goBack">
          < 返回
        </button>
        <h1 class="notification-title">消息通知</h1>
      </div>
      <div class="notification-actions">
        <button 
          v-if="hasUnreadNotifications" 
          class="mark-all-read-btn"
          @click="markAllAsRead"
        >
          全部标记为已读
        </button>
      </div>
    </div>

    <div class="notification-content">
      <div v-if="notifications.length === 0" class="no-notifications">
        <i class="fas fa-bell-slash notification-icon"></i>
        <p>暂无通知</p>
      </div>

      <div v-else class="notification-list">
        <div
          v-for="notification in notifications"
          :key="notification.id"
          class="notification-item"
          :class="{ 'unread': !notification.isRead }"
          @click="markAsRead(notification)"
        >
          <div class="notification-icon-wrapper">
            <i :class="getNotificationIcon(notification.type)"></i>
          </div>
          <div class="notification-details">
            <div class="notification-header-info">
              <h3 class="notification-title-text">{{ notification.title }}</h3>
              <span class="notification-time">{{ formatTime(notification.time) }}</span>
            </div>
            <p class="notification-content-text">{{ notification.content }}</p>
            <div class="notification-footer">
              <span class="notification-type">{{ getNotificationTypeText(notification.type) }}</span>
              <span v-if="!notification.isRead" class="unread-badge">未读</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Notifications',
  data() {
    return {
      notifications: []
    }
  },
  computed: {
    hasUnreadNotifications() {
      return this.notifications.some(notification => !notification.isRead);
    },
    unreadCount() {
      return this.notifications.filter(notification => !notification.isRead).length;
    }
  },
  mounted() {
    this.loadNotifications();
  },
  methods: {
    loadNotifications() {
      // 从本地存储加载通知
      const savedNotifications = localStorage.getItem('notifications');
      if (savedNotifications) {
        try {
          this.notifications = JSON.parse(savedNotifications);
        } catch (e) {
          
          this.initializeDefaultNotifications();
        }
      } else {
        this.initializeDefaultNotifications();
      }
    },
    
    initializeDefaultNotifications() {
      // 初始化默认通知
      this.notifications = [
        {
          id: 1,
          title: '系统维护通知',
          content: '系统将于今晚00:00-02:00进行例行维护，请提前做好数据保存工作。',
          type: 'system',
          time: '2025-10-15 14:30:00',
          isRead: false
        },
        {
          id: 2,
          title: '数据备份完成',
          content: '今日数据备份已完成，备份文件已存储至指定位置。',
          type: 'info',
          time: '2025-10-15 02:00:00',
          isRead: true
        },
        {
          id: 3,
          title: '安全警报',
          content: '检测到异常登录尝试，请检查账户安全。',
          type: 'warning',
          time: '2025-10-14 20:15:00',
          isRead: false
        },
        {
          id: 4,
          title: '新功能上线',
          content: '风险评估模块新增智能分析功能，欢迎体验。',
          type: 'info',
          time: '2025-10-14 10:00:00',
          isRead: true
        },
        {
          id: 5,
          title: '数据同步完成',
          content: '与总部数据库同步已完成，数据已更新至最新状态。',
          type: 'success',
          time: '2025-10-14 06:00:00',
          isRead: true
        }
      ];
      this.saveNotifications();
    },
    
    saveNotifications() {
      try {
        localStorage.setItem('notifications', JSON.stringify(this.notifications));
        // 更新Header中的未读数量
        localStorage.setItem('unreadNotificationCount', this.unreadCount.toString());
      } catch (e) {
        
      }
    },
    
    markAsRead(notification) {
      if (!notification.isRead) {
        notification.isRead = true;
        this.saveNotifications();
        // 触发更新Header中的未读数量
        window.dispatchEvent(new Event('notification-read'));
      }
    },
    
    goBack() {
      this.$router.go(-1);
    },

    markAllAsRead() {
      this.notifications.forEach(notification => {
        notification.isRead = true;
      });
      this.saveNotifications();
      // 触发更新Header中的未读数量
      window.dispatchEvent(new Event('notification-read'));
    },
    
    getNotificationIcon(type) {
      const icons = {
        system: 'fas fa-cog',
        info: 'fas fa-info-circle',
        warning: 'fas fa-exclamation-triangle',
        success: 'fas fa-check-circle'
      };
      return icons[type] || 'fas fa-bell';
    },
    
    getNotificationTypeText(type) {
      const types = {
        system: '系统通知',
        info: '信息',
        warning: '警告',
        success: '成功'
      };
      return types[type] || '通知';
    },
    
    formatTime(timeString) {
      const now = new Date();
      const time = new Date(timeString);
      const diffMs = now - time;
      const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
      const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
      const diffMinutes = Math.floor(diffMs / (1000 * 60));
      
      if (diffDays > 0) {
        return `${diffDays}天前`;
      } else if (diffHours > 0) {
        return `${diffHours}小时前`;
      } else if (diffMinutes > 0) {
        return `${diffMinutes}分钟前`;
      } else {
        return '刚刚';
      }
    }
  }
}
</script>

<style scoped>
.notification-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.notification-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e2e8f0;
}

.header-left {
  display: flex;
  align-items: center;
}

.back-button {
  background-color: #3182ce;
  border: none;
  color: white;
  font-size: 10px;
  margin-right: 12px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.back-button:hover {
  background-color: #2c5282;
  transform: translateY(-1px);
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.notification-title {
  font-size: 24px;
  font-weight: 600;
  color: #1a202c;
}

.mark-all-read-btn {
  padding: 8px 16px;
  background-color: #4299e1;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.2s;
}

.mark-all-read-btn:hover {
  background-color: #3182ce;
}

.no-notifications {
  text-align: center;
  padding: 60px 20px;
  color: #a0aec0;
}

.notification-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.notification-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.notification-item {
  display: flex;
  padding: 16px;
  border-radius: 8px;
  background-color: #f7fafc;
  border: 1px solid #e2e8f0;
  cursor: pointer;
  transition: all 0.2s;
}

.notification-item:hover {
  background-color: #edf2f7;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}

.notification-item.unread {
  background-color: #ebf8ff;
  border-left: 4px solid #4299e1;
}

.notification-icon-wrapper {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #bee3f8;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  flex-shrink: 0;
}

.notification-icon-wrapper i {
  color: #4299e1;
  font-size: 18px;
}

.notification-details {
  flex: 1;
}

.notification-header-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
}

.notification-title-text {
  font-size: 16px;
  font-weight: 600;
  color: #1a202c;
  margin: 0;
}

.notification-time {
  font-size: 12px;
  color: #718096;
}

.notification-content-text {
  font-size: 14px;
  color: #4a5568;
  margin: 0 0 12px 0;
  line-height: 1.5;
}

.notification-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.notification-type {
  font-size: 12px;
  color: #718096;
  background-color: #e2e8f0;
  padding: 2px 8px;
  border-radius: 12px;
}

.unread-badge {
  font-size: 12px;
  color: #fff;
  background-color: #e53e3e;
  padding: 2px 8px;
  border-radius: 12px;
}
</style>